package com.estudy.service;

import com.alibaba.fastjson.JSON;
import com.estudy.bean.CourseDO;
import com.estudy.bean.CourseDOExample;
import com.estudy.bean.CourseKnowledgeDO;
import com.estudy.bean.CourseKnowledgeDOExample;
import com.estudy.dao.CourseDOMapper;
import com.estudy.dao.CourseKnowledgeDOMapper;
import com.estudy.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author: yi.jianhua
 * @create: 2019/09/05 15:20
 */
@Service
public class CourseService {
    @Autowired
    private CourseDOMapper courseDOMapper;
    @Autowired
    private CourseKnowledgeDOMapper courseKnowledgeDOMapper;

    public List<Course> queryAllCourses() {
        CourseDOExample courseDOExample = new CourseDOExample();
        List<CourseDO> courseDOS = courseDOMapper.selectByExample(courseDOExample);
        List<Course> courses = convert2Course(courseDOS);
        System.out.println(JSON.toJSONString(courses));
        return courses;
    }

    private List<Course> convert2Course(List<CourseDO> courseDOS) {
        if (CollectionUtils.isEmpty(courseDOS)) {
            return Collections.emptyList();
        }
        List<Course> courses = new ArrayList<Course>(courseDOS.size());
        for (CourseDO courseDO : courseDOS) {
            Course course = new Course();
            course.setChapter(courseDO.getChapter());
            course.setCid(courseDO.getCourse_no());
            course.setCourseName(courseDO.getCourse_name());
            course.setCourseDesc(courseDO.getCourse_desc());
            course.setCreateTime(courseDO.getCreate_time());
            course.setId(courseDO.getId());
            course.setStatus(courseDO.getStatus());
            course.setChapterName("第 " + courseDO.getChapter() + " 章");
            courses.add(course);
        }
        return courses;
    }

    public void addCourse(Course course) {
        CourseDO courseDO = convert2DO(course);
        courseDOMapper.insertSelective(courseDO);
    }

    private CourseDO convert2DO(Course course) {
        CourseDO result = new CourseDO();
        result.setChapter(course.getChapter());
        result.setCourse_desc(course.getCourseDesc());
        result.setCourse_name(course.getCourseName());
        result.setCourse_no(course.getCid());
        result.setCreate_time(course.getCreateTime());
        result.setStatus(course.getStatus());
        return result;
    }

    public void deleteCourseById(String id) {
        courseDOMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    public void addChapter(String cid) {
        CourseDOExample courseDOExample = new CourseDOExample();
        CourseDOExample.Criteria criteria = courseDOExample.createCriteria();
        criteria.andCourse_noEqualTo(cid);
        courseDOExample.setOrderByClause("chapter desc");
        List<CourseDO> courseDOS = courseDOMapper.selectByExample(courseDOExample);
        CourseDO courseDO = courseDOS.get(0);
        System.out.println(JSON.toJSONString(courseDO));

        int newChapter = courseDO.getChapter() + 1;
        courseDO.setChapter(newChapter);
        courseDO.setCreate_time(System.currentTimeMillis());
        courseDOMapper.insertSelective(courseDO);
    }

    public void addChapterCourseMap(String id, List<String> fidList) {
        CourseKnowledgeDOExample courseKnowledgeDOExample = new CourseKnowledgeDOExample();
        CourseKnowledgeDOExample.Criteria criteria = courseKnowledgeDOExample.createCriteria();
        criteria.andCourse_idEqualTo(id);
        courseKnowledgeDOMapper.deleteByExample(courseKnowledgeDOExample);
        for (String fid : fidList) {
            CourseKnowledgeDO courseKnowledgeDO = new CourseKnowledgeDO();
            courseKnowledgeDO.setCourse_id(id);
            courseKnowledgeDO.setKn_id(fid);
            courseKnowledgeDOMapper.insert(courseKnowledgeDO);
        }
    }

    public Set<String> querySelectFiledById(String id) {
        CourseKnowledgeDOExample courseKnowledgeDOExample = new CourseKnowledgeDOExample();
        CourseKnowledgeDOExample.Criteria criteria = courseKnowledgeDOExample.createCriteria();
        criteria.andCourse_idEqualTo(id);
        List<CourseKnowledgeDO> courseKnowledgeDOS = courseKnowledgeDOMapper.selectByExample(courseKnowledgeDOExample);
        if (CollectionUtils.isEmpty(courseKnowledgeDOS)) {
            return Collections.emptySet();
        }
        Set<String> result = new HashSet<String>();
        for (CourseKnowledgeDO DO : courseKnowledgeDOS) {
            result.add(DO.getKn_id());
        }
        return result;
    }

    public List<Course> queryAllOnlineCourse() {
        CourseDOExample courseDOExample = new CourseDOExample();
        CourseDOExample.Criteria criteria = courseDOExample.createCriteria();
        criteria.andStatusEqualTo(1);
        List<CourseDO> courseDOS = courseDOMapper.selectByExample(courseDOExample);
        Set<String> allCourse = new HashSet<String>();
        List<CourseDO> result = new ArrayList<CourseDO>();
        for (CourseDO courseDO : courseDOS) {
            if (!allCourse.contains(courseDO.getCourse_no())) {
                result.add(courseDO);
                allCourse.add(courseDO.getCourse_no());
            }
        }
        return convert2Course(result);
    }
}
